﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Globalization;
using System.Web.Security;
using System.Linq;

namespace ProjetDotNet2012.Models
{
    /// AccountModels.cs
    /// 
    /// <summary>
    /// Modèle d'un compte utilisateur 
    /// </summary>
    ///
    /// <author>
    /// Yacine HAMEK
    /// </author>

    public interface IAccountManagerRepository
    {
        void ChangeEmail(EmailModel model);
    }

    public class EntityAccountManagerRepository : IAccountManagerRepository
    {
        private ProjetDotNet2012DBEntities _entities = new ProjetDotNet2012DBEntities();

        public void ChangeEmail(EmailModel model)
        {
            (from c in _entities.User
                         where c.Email == model.OldEmail
                         select c).FirstOrDefault().Email = model.NewEmail;

            _entities.SaveChanges();
        }
    }

    public class UsersContext : DbContext
    {
        public UsersContext()
            : base("ProjetDotNet2012DB")
        {
        }

        public DbSet<UserProfile> UserProfiles { get; set; }
    }

    [Table("User")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
    }

    public class LocalPasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Mot de passe actuel")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "La chaîne {0} doit comporter au moins {2} caractères.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Nouveau mot de passe")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirmer le nouveau mot de passe")]
        [Compare("NewPassword", ErrorMessage = "Le nouveau mot de passe et le mot de passe de confirmation ne correspondent pas.")]
        public string ConfirmPassword { get; set; }
    }

    public class LoginModel
    {
        [Required]
        [Display(Name = "Nom d'utilisateur")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Mot de passe")]
        public string Password { get; set; }

        [Display(Name = "Mémoriser le mot de passe ?")]
        public bool RememberMe { get; set; }
    }

    public class RegisterModel
    {
        [Required]
        [Display(Name = "Nom d'utilisateur")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "La chaîne {0} doit comporter au moins {2} caractères.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Mot de passe")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirmer le mot de passe ")]
        [Compare("Password", ErrorMessage = "Le mot de passe et le mot de passe de confirmation ne correspondent pas.")]
        public string ConfirmPassword { get; set; }

        [Required]
        [Display(Name = "Adresse email")]
        public string Email { get; set; }
    }

    public class EmailModel
    {
        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Adresse email actuelle")]
        public string OldEmail { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Nouvelle adresse email")]
        public string NewEmail { get; set; }
    }
}
